package com.gzh.teaching.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gzh.core.chche.service.CacheManage;
import com.gzh.core.dao.mapper.AcademyMapper;
import com.gzh.core.dao.mapper.ChooseMapper;
import com.gzh.core.dao.mapper.ClassMapper;
import com.gzh.core.dao.mapper.UserMapper;
import com.gzh.core.dao.po.AcademyEntity;
import com.gzh.core.dao.po.ChooseEntity;
import com.gzh.core.dao.po.ClassEntity;
import com.gzh.core.dao.po.UserEntity;
import com.gzh.core.util.NumericHelper;
import com.gzh.core.util.StringUtils;
import com.gzh.teaching.model.vo.QueryOut;
import com.gzh.teaching.model.vo.QueryUserIn;
import com.gzh.teaching.model.vo.QueryUserResult;
import com.gzh.teaching.service.AdminService;
import com.gzh.teaching.service.dto.CourseUserOut;
import com.gzh.teaching.service.dto.QueryIn;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author guanzh
 * @Date 2022/3/3 17:13
 * @Description
 */
@Slf4j
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AcademyMapper academyMapper;
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private CacheManage cacheManage;
    @Autowired
    private ChooseMapper chooseMapper;

    @Override
    public QueryUserResult queryAllUser(QueryUserIn in) {
        QueryUserResult result = new QueryUserResult();

        UserEntity userQuery = new UserEntity();
        userQuery.setUserId(in.getUserId());
        UserEntity user = userMapper.selectOne(userQuery);
        Integer schoolId = user.getSchoolId();

        Condition userCondition = new Condition(UserEntity.class);
        Example.Criteria criteria = userCondition.createCriteria();
        if (in.getGrade() != null){
            criteria.andEqualTo("grade", in.getGrade());
        }
        if (in.getAcademyId() != null){
            criteria.andEqualTo("academyId", in.getAcademyId());
        }
        if (in.getClassId() != null){
            criteria.andEqualTo("classId", in.getClassId());
        }
       if (in.getStuId() != null && in.getStuId() != ""){
           criteria.orLike("stuId", "%" + in.getStuId().trim() + "%");
           criteria.orLike("fullName", "%" + in.getStuId().trim() + "%");
       }
        criteria.andIn("roleId", in.getRoleId() == null ? Arrays.asList(0, 1) : Arrays.asList(in.getRoleId()));
        criteria.andEqualTo("schoolId", schoolId);
        userCondition.orderBy("grade");
        userCondition.orderBy("academyId");
        userCondition.orderBy("classId");

        PageHelper.startPage(in.getPageNum(), in.getPageSize());
        Page page = PageHelper.getLocalPage();
        List<UserEntity> userEntityList = userMapper.selectByCondition(userCondition);
        result.setTotal(page.getTotal());
        result.setPageNum(page.getPageNum());
        result.setPages(page.getPages());
        result.setPageSize(page.getPageSize());
        //封装
        result.setList(userEntityList);
        result.setSchoolId(schoolId);
        result.setSchoolName(cacheManage.getSchoolCache().getObject(StringUtils.toString(schoolId)).getSchoolName());
        return result;
    }

    @Override
    public void deleteUser(Integer userId) {
        Condition userCondition = new Condition(UserEntity.class);
        userCondition.createCriteria()
                .andEqualTo("userId", userId);
        userMapper.deleteByCondition(userCondition);

        Condition chooseCondition = new Condition(ChooseEntity.class);
        chooseCondition.createCriteria().andEqualTo("userId", userId);
        chooseMapper.deleteByCondition(chooseCondition);
    }

    @Override
    public void editUser(UserEntity user) {
        Condition userCondition = new Condition(UserEntity.class);
        userCondition.createCriteria()
                .andEqualTo("userId", user.getUserId());
        userMapper.updateByCondition(user,userCondition);
    }

    @Override
    public QueryOut queryAcademy(QueryIn in) {
        QueryOut out = new QueryOut();
        PageHelper.startPage(in.getPageNum(), in.getPageSize());
        Page page = PageHelper.getLocalPage();
        Condition condition = new Condition(AcademyEntity.class);
        condition.createCriteria().andEqualTo("status",in.getStatus());
        List<AcademyEntity> academyEntityList = academyMapper.selectByCondition(condition);
        out.setTotal(page.getTotal());
        out.setPageNum(page.getPageNum());
        out.setPages(page.getPages());
        out.setPageSize(page.getPageSize());
        out.setAList(academyEntityList);
        return out;
    }

    @Override
    public QueryOut queryClass(QueryIn in) {
        QueryOut out = new QueryOut();
        PageHelper.startPage(in.getPageNum(), in.getPageSize());
        Page page = PageHelper.getLocalPage();

        Condition condition = new Condition(ClassEntity.class);
        condition.createCriteria().andEqualTo("tacademyId",in.getTacademyId())
                        .andEqualTo("status",in.getStatus());
        condition.orderBy("tacademyId");
        List<ClassEntity> classEntities = classMapper.selectByCondition(condition);
        out.setTotal(page.getTotal());
        out.setPageNum(page.getPageNum());
        out.setPages(page.getPages());
        out.setPageSize(page.getPageSize());
        out.setCList(classEntities);
        return out;
    }

    @Override
    public void editInfo(QueryIn in) {
        if (in.getClassId() != null){
            ClassEntity classEntity = new ClassEntity();
            Condition condition = new Condition(ClassEntity.class);
            condition.createCriteria().andEqualTo("classId",in.getClassId());
            classEntity.setStatus(in.getStatus());
            classMapper.updateByConditionSelective(classEntity,condition);
        }
        if (in.getTacademyId() != null){
            Condition condition = new Condition(AcademyEntity.class);
            condition.createCriteria().andEqualTo("academyId",in.getTacademyId());
            AcademyEntity academyEntity = new AcademyEntity();
            academyEntity.setStatus(in.getStatus());
            academyMapper.updateByConditionSelective(academyEntity,condition);
        }
    }

    @Override
    public void insertAcademy(AcademyEntity param) {
        AcademyEntity academyEntity = new AcademyEntity();
        academyEntity.setAcademyName(param.getAcademyName());
        academyEntity.setStatus(1);
        academyMapper.insertSelective(academyEntity);
    }

    @Override
    public void insertClass(ClassEntity param) {
        ClassEntity classEntity = new ClassEntity();
        classEntity.setClassName(param.getClassName());
        classEntity.setTacademyId(param.getTacademyId());
        classEntity.setStatus(1);
        classMapper.insertSelective(classEntity);
    }
}
